package com.ruoyi.produce.base.shift.util;

import com.ruoyi.produce.base.shift.domain.ProduceShiftInfo;
import com.ruoyi.produce.base.shift.service.IProduceShiftInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class banciScheduler {
    @Autowired
    private IProduceShiftInfoService produceShiftInfoService;

    /**
     * 每半小时执行一下
     * @throws SQLException
     */

//    @Scheduled(cron = "0/30 * * * * ? ")
    @Scheduled(cron = "0 0/30 * * * ? ")
    public synchronized void banciTasks1() throws SQLException {
        ProduceShiftInfo id1 = produceShiftInfoService.selectProduceShiftInfoById(produceShiftInfoService.createDayShiftId());
        ProduceShiftInfo id2 = produceShiftInfoService.selectProduceShiftInfoById(produceShiftInfoService.createNightShiftId());
        // 当天班次
        if (id1 == null && id2 == null) {
            LocalDateTime localDateTime = LocalDateTime.now();
            DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyyMMdd");
            DateTimeFormatter dtf1=DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String strDate=localDateTime.format(dtf);
            String nowDate = localDateTime.format(dtf1);
            String oldTime = null;
            String now = null;
            String name = "白班";
//            String url="jdbc:mysql://192.168.1.12:3306/erp?"
//                    +"user=client&password=msd123456&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
            String url="jdbc:mysql://rm-bp15worrp5fg8k71jko.mysql.rds.aliyuncs.com/temp2?"
                    +"user=lsy&password=Lsy12345678&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
            SqlConnect sqlConnect = new SqlConnect();
            sqlConnect.connsql(url);
            for(int i=1;i<=2;i++) {
                if (i == 2) {
                    name = "夜班";
                }
                ResultSet rs = sqlConnect.selectsql("select start_time from produce_shift_info where shift_info_no = '" + i + "' order by create_time desc limit 1");
                while (rs.next()){
                    oldTime = rs.getString(1);
                }
                System.out.println("oldTime" + oldTime);
                String[] timeSplit  = oldTime.split(" ");
                System.out.println("nowDate" + nowDate);
                now = nowDate+" "+timeSplit[1];
                String shiftInfoId = strDate+"0"+i;
                sqlConnect.insertsql("INSERT INTO produce_shift_info (shift_info_no, shift_info_id, shift_info_name, start_time, duration_time, create_time) VALUES ('"
                        + i + "', '" + shiftInfoId + "', '" + name + "' , '" + now + "', 12, SYSDATE())" );
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            sqlConnect.deconnsql();
        }
    }
}
